//
//  DayGuaViewController.m
//  JxCarios
//
//  Created by xcq on 15/1/21.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "DayGuaViewController.h"
#import "LoginViewController.h"
#import "CarAroundDataSource.h"
#import "CLUIBaseNavigationController.h"
@interface DayGuaViewController () <UIWebViewDelegate,UIAlertViewDelegate>{
    CarAroundDataSource *carAround;
}

@end

@implementation DayGuaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([RextExtends getScreenFrame]), CGRectGetHeight([RextExtends getScreenFrame]) - 44)];

//    _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
//    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
}

- (void)viewWillAppear:(BOOL)animated {

    if(![Common currenLoginStatus]){
        [Common showLocalAlert:@"提示" message:@"请先登录" delegate:self andTag:100];
       
        return;
    }
    [self netRequest];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        self.tabBarController.selectedIndex = 0;
    }else {
        LoginViewController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginView"];
        CLUIBaseNavigationController *base = [[CLUIBaseNavigationController alloc] initWithRootViewController:login];
        login.hidesBottomBarWhenPushed = YES;
        [self presentViewController:base animated:YES completion:^{
            
        }];
    }
}

- (void)netRequest {
    if (!carAround) {
        carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    
    [carAround getGuaGuaLe:^(id result) {
        _url = [NSURL URLWithString:[result customObjectForkey:@"url"]];
        
         _webView.delegate = self;
         NSURLRequest *request = [NSURLRequest requestWithURL:_url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15 ];
        [self.webView loadRequest:request];

    } anfFail:^(id result) {
        
    }];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.view makeToastActivity];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    NSString *js_fit_code = [NSString stringWithFormat:@"var meta = document.createElement('meta');"
                             "meta.name = 'viewport';"
                             "meta.content = 'width=device-width, initial-scale=1.0,minimum-scale=0.1, maximum-scale=2.0, user-scalable=yes';"
                             "document.getElementsByTagName('head')[0].appendChild(meta);"
                             ];
    [webView stringByEvaluatingJavaScriptFromString:js_fit_code];
    NSLog(@"%s after__ %@",__FUNCTION__ ,NSStringFromCGSize(_webView.scrollView.contentSize));
    _webView.scrollView.zoomScale = 0.2;
     _webView.scrollView.scrollEnabled = NO;
     [_webView setNeedsDisplay];
    [self.view hideToastActivity];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
